# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId


class AppInfo(db.Model, JsonEncoder):
    __tablename__ = "apps"
    id = db.Column(db.String, primary_key = True)
    app_name = db.Column(db.String)
    host = db.Column(db.String)
    http_port = db.Column(db.String)
    jmx_port = db.Column(db.String)
    agent_port = db.Column(db.String)
    app_type = db.Column(db.String)
    entrance_id = db.Column(db.String)
    entrance_type = db.Column(db.String)
    current_status = db.Column(db.String)
    expect_status = db.Column(db.String)
    error_desc = db.Column(db.String)
    env_name = db.Column(db.String)
    zone_code = db.Column(db.String)
    group = db.Column(db.String)
    cloudlink_status = db.Column(db.String)
    dubbo_status = db.Column(db.String)
    update_time = db.Column(db.DATETIME)
    is_resource = db.Column(db.String)
    is_overtime = db.Column(db.String)

    def __init__(self, app_name = None, host = None, http_port = None, jmx_port = None, agent_port = None,
                 app_type = None, entrance_id = None, entrance_type = None, current_status = None, expect_status = None,
                 error_desc = None, zone_code = None, update_time = None, group = None, cloudlink_status = None,
                 dubbo_status = None, env_name = None, is_resource = '0', is_overtime = '1'):
        self.id = createId('ap')
        self.app_name = app_name
        self.host = host
        self.http_port = http_port
        self.jmx_port = jmx_port
        self.agent_port = agent_port
        self.app_type = app_type
        self.entrance_id = entrance_id
        self.entrance_type = entrance_type
        self.current_status = current_status
        self.expect_status = expect_status
        self.error_desc = error_desc
        self.env_name = env_name
        self.zone_code = zone_code
        self.group = group
        self.update_time = update_time
        self.cloudlink_status = cloudlink_status
        self.dubbo_status = dubbo_status
        self.is_resource = is_resource
        self.is_overtime = is_overtime

    def to_dict(self):
        return {
            'id': self.id,
            'app_name': self.app_name,
            'host': self.host,
            'http_port': self.http_port,
            'jmx_port': self.jmx_port,
            'agent_port': self.agent_port,
            'app_type': self.app_type,
            'entrance_id': self.entrance_id,
            'entrance_type': self.entrance_type,
            'current_status': self.current_status,
            'expect_status': self.expect_status,
            'error_desc': self.error_desc,
            'env_name': self.env_name,
            'zone_code': self.zone_code,
            'group': self.group,
            'cloudlink_status': self.cloudlink_status,
            'dubbo_status': self.dubbo_status,
            'is_resource': self.is_resource,
            'is_overtime': self.is_overtime,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
        }
